package com.xwd.utils;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName PropertyUtil
 * @Description: com.xwd.utils-读取配置文件的属性值工具类
 * @Auther: xiwd
 * @Date: 2022/2/13 - 02 - 13 - 13:28
 * @version: 1.0
 */
public class PropertyUtil {
    //properties
    private static Logger logger;
    private Properties properties;

    //block
    static {
        //实例化Logger
        logger = Logger.getLogger(PropertyUtil.class);
        //打印日志信息
        logger.info("log4j init SUCCESSFULLY!");
    }

    //setter

    //getter

    //constructors

    /**
     * 加载配置文件
     * @param configFile
     */
    public PropertyUtil(String configFile) {
        //实例化属性
        this.properties = new Properties();
        //获取输入流
        InputStream inputStream = PropertyUtil.class.getResourceAsStream(configFile);
        //加载配置文件
        try {
            properties.load(inputStream);
            logger.fatal("configFile-"+configFile+" load SUCCESSFULLY!");
        } catch (IOException e) {
            e.printStackTrace();
            logger.fatal("configFile-"+configFile+" load FAILED!");
        }

    }

    //methods
    /**
     * 根据属性关键字key获取属性值value
     * @param key 属性关键字key
     * @return 属性值value
     */
    public String getPropertyValue(String key){
        return this.properties.getProperty(key);
    }
}
